package com.study.service; 

import java.util.List;
import javax.annotation.Resource;
import com.study.dao.UserDao;
import com.study.entity.User;
import com.study.util.Tools;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;  
/**  
 *  
 */  
@Transactional
@Service
public class UserService  
{  
    /********** 注入UserDao ***********/  
    @Resource(name="userDao")
	private UserDao userDao;
	

	//返回的json数据的key
	private final static String RE_CODE_STR = "code";
	private final static String RE_DATA_STR = "data";
	private final static String RE_PAGE_STR = "pages";//数据共有多少页
	private final static String RE_COUNT_STR = "count";//数据的总数
	
	//返回的json数据的value
	//返回 0 表示操作成功
	private final static int RE_CODE_0 = 0;
	//返回 1表示操作失败
	private final static int RE_CODE_1 = 1;
	//返回 2表示没有数据
	private final static int RE_CODE_2 = 2;
    
    //增加一个User
    public String saveUser(User user){
		JSONObject resultObj = new JSONObject();
		userDao.saveUser(user);
		
		resultObj.put(RE_CODE_STR, RE_CODE_0);
		return resultObj.toString();
		
	}
	
	
	//根据id查询User
    public String getUserById(String id){
    	JSONObject resultObj = new JSONObject();
        
        User result = userDao.getUserById(id);
        if(result==null){
            resultObj.put(RE_CODE_STR, RE_CODE_2);
        }else{
            resultObj.put(RE_CODE_STR, RE_CODE_0);
            JSONObject jb = new JSONObject(result);
            resultObj.put(RE_DATA_STR, jb);
        }
        
        return resultObj.toString();
    }
	
	
	
	
	
	//注册一个User
    public String registerUser(User user){
        JSONObject resultObj = new JSONObject();
        
        List<User> list = userDao.listUser(user,null,null);
        if(list==null || list.size()==0){
            userDao.saveUser(user);
            resultObj.put(RE_CODE_STR, RE_CODE_0);
        }else{
            resultObj.put(RE_CODE_STR, RE_CODE_1);
            resultObj.put(RE_DATA_STR, "该用户名已存在");
        }
        
        return resultObj.toString();
    }
    
    
    //修改密码
    public String modifyPass(String userid, String oripass, String newpass){
    	JSONObject resultObj = new JSONObject();
        User user = userDao.getUserById(userid);
        String pass = user.getPassword();
        if (pass.equals(oripass)) {
        	userDao.updatePasswordInUser(userid,newpass);
        	resultObj.put(RE_CODE_STR, RE_CODE_0);
        } else {
        	resultObj.put(RE_CODE_STR, RE_CODE_1);
            resultObj.put(RE_DATA_STR, "原始密码不正确");
        }
        return resultObj.toString();
    }
	
	//分页条件查询User
	public String listUser(User user, String page, String number){
		JSONObject resultObj = new JSONObject();
		String from = null;
		if (Tools.notEmpty(page) && Tools.notEmpty(number)) {
			from = String.valueOf((Integer.parseInt(page)-1)*Integer.parseInt(number));
		}
		
	    List<User> listPage = userDao.listUser(user,from, number);
	    if (listPage.isEmpty()) {
			resultObj.put(RE_CODE_STR, RE_CODE_2);
		} else {
			JSONArray array = new JSONArray();
			for(int i=0,len=listPage.size();i<len;i++){
				User item = listPage.get(i);
				array.put(i, new JSONObject(item));
			}
		
	        resultObj.put(RE_CODE_STR, RE_CODE_0);
			resultObj.put(RE_DATA_STR, array);
			if (Tools.notEmpty(page) && Tools.notEmpty(number)) {
			    long count = userDao.getUserCount(user);
				resultObj.put(RE_PAGE_STR, Math.ceil(count/Double.parseDouble(number)));
				resultObj.put(RE_COUNT_STR, count);
			}
		}
		return resultObj.toString();
	}
	

		
	//根据id删除User
	public String removeUserById(String id){
		JSONObject resultObj = new JSONObject();
		boolean res =  userDao.removeUserById(id);
		if (res) {
			resultObj.put(RE_CODE_STR, RE_CODE_0);
		} else {
			resultObj.put(RE_CODE_STR, RE_CODE_1);
		}
		return resultObj.toString();
	}
	
	//更新User
	public String updateUser(User user){
		JSONObject resultObj = new JSONObject();
		
	    userDao.updateUser(user);
	    resultObj.put(RE_CODE_STR, RE_CODE_0);
	    return resultObj.toString();
	}
	
	//更新User表中的password
	public String updatePasswordInUser(String id, String password){
		JSONObject resultObj = new JSONObject();
		boolean res =  userDao.updatePasswordInUser(id, password);
		if (res) {
			resultObj.put(RE_CODE_STR, RE_CODE_0);
		} else {
			resultObj.put(RE_CODE_STR, RE_CODE_1);
		}
		return resultObj.toString();
	}
	
    
}